import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    console.log(this.props.match.params.id);
    return <div>首页</div>;
  }
}
class About extends Component {
  render() {
    // console.log(this.props.location.search);
    const {
      location: { search },
    } = this.props;
    const obj = new URLSearchParams(search);
    console.log(obj.get("a"));
    return <div>关于页</div>;
  }
}
class Mine extends Component {
  render() {
    console.log(this.props);
    return <div>我的页</div>;
  }
}

class Basic extends Component {
  state = {};
  render() {
    return (
      <>
        <h3>路由传参</h3>
        <ul>
          <li>
            <Link to="/home/234555">home</Link>
          </li>
          <li>
            <Link to="/about?a=3&b=4">about</Link>
          </li>
          <li>
            <Link
              to={{
                pathname: "/mine",
                search: "?x=10&y=20",
                hash: "#abc",
                state: { msg: "hello" },
              }}
            >
              mine
            </Link>
          </li>
        </ul>

        <Switch>
          {/* params传参,动态路由 */}
          <Route path="/home/:id" component={Home} exact></Route>
          {/* query传参 */}
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default Basic;
